package com.aimuceliang.aimuceliang.api;

import com.aimuceliang.aimuceliang.Constants;
import com.aimuceliang.aimuceliang.bean.HttpResult;
import com.aimuceliang.aimuceliang.net.RequestInterceptor;
import com.aimuceliang.aimuceliang.net.ResponseInterceptor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.Subscription;

/**
 * Created by ziabo on 2017/7/27.
 * 请求服务类
 */

public class ApiService {
    private ApiInterface mApiInterface;

    private ApiService() {
        //HTTP log
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        //RequestInterceptor
        RequestInterceptor requestInterceptor = new RequestInterceptor();

        //ResponseInterceptor
        ResponseInterceptor responseInterceptor = new ResponseInterceptor();

        //OkHttpClient
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(Constants.TIME_OUT, TimeUnit.SECONDS)
                .addInterceptor(requestInterceptor)
                .addInterceptor(responseInterceptor);
        if (Constants.DEBUG_MODE) {
            builder.addInterceptor(httpLoggingInterceptor);
        }
        OkHttpClient mOkHttpClient = builder.build();
        //Retrofit
        Retrofit mRetrofit = new Retrofit.Builder()
                .client(mOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(Constants.IP)
                .build();
        //ApiInterface
        mApiInterface = mRetrofit.create(ApiInterface.class);
    }

    //Singleton
    private static class SingletonHolder {
        private static final ApiService INSTANCE = new ApiService();
    }

    //Instance
    public static ApiService getApiService() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 登录
     */
    public Subscription login(Subscriber<HttpResult> subscriber, String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", username);
        map.put("pwd", password);
        return mApiInterface.login(map)
                .compose(new SchedulersTransformer<>())
                .subscribe(subscriber);
    }

    /**
     * 登录
     */
    public Subscription register(Subscriber<HttpResult> subscriber, String company, String name, String email, String tel) {
        Map<String, Object> map = new HashMap<>();
        map.put("company", company);
        map.put("name", name);
        map.put("email", email);
        map.put("tel", tel);
        return mApiInterface.register(map)
                .compose(new SchedulersTransformer<>())
                .subscribe(subscriber);
    }

    /**
     * 登录
     */
    public Subscription modifyPwd(Subscriber<HttpResult> subscriber, String user, String pwdold, String pwdnew) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("pwdold", pwdold);
        map.put("pwdnew", pwdnew);
        return mApiInterface.modify_pwd(map)
                .compose(new SchedulersTransformer<>())
                .subscribe(subscriber);
    }

    public Subscription savedict(Subscriber<HttpResult> subscriber, String company) {
        Map<String, Object> map = new HashMap<>();
        map.put("company", company);
        return mApiInterface.getDict(map).compose(new SchedulersTransformer<>()).subscribe(subscriber);
    }
}
